package com.taldh.algorithm.二分查找法;

import org.junit.Assert;
import org.junit.Test;

public class FindDuplicate {
    public int findDuplicate(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            if (nums[Math.abs(nums[i]) - 1] > 0) {
                nums[Math.abs(nums[i]) - 1] = - nums[Math.abs(nums[i]) - 1];
            } else {
                return Math.abs(nums[i]);
            }
        }
        return -1;
    }

    @Test
    public void test() {
        Assert.assertEquals(2, findDuplicate(new int[]{1,3,4,2,2}));
    }

    @Test
    public void test1() {
        Assert.assertEquals(3, findDuplicate(new int[]{3,1,3,4,2}));
    }
}
